/***************************************************************************
 *   Copyright (C) 2008 by Milambert									   *
 *   nexus6b@gmail.com   												   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include "config.h"
#include "common.h"
#include "confHandler.h"

#define DELIMITER "="

//global config vars
int sockPort;
int myPort;
char *myUser;
char *myPwd;
char *myHost;
char *myDB;
double timer;

//parse arguments
void readArg(int argc, char *argv[]){
	int c;

	while(1){
		static struct option lopt[] =
			{
				{"help",	no_argument, 0, 'a'},		//display help
				{"port",	required_argument, 0, 's'},	//socket port
				{"myport",	required_argument, 0, 'p'},	//MySQL port
				{"myuser",	required_argument, 0, 'u'},	//MySQL user
				{"mypwd",	required_argument, 0, 'w'},	//MySQL pwd
				{"myhost",	required_argument, 0, 'h'},	//MySQL host
				{"mydb",	required_argument, 0, 'd'},	//MySQl DB
				{"time",	required_argument, 0, 't'},	//probe time
				{"version",	no_argument, 0, 'v'},
				{0, 0, 0, 0}
			};

		int opt_index = 0;

		c = getopt_long(argc, argv, "a:s:p:u:w:h:d:v", lopt, &opt_index);

		//end of arguments
		if(c == -1)
			break;

		switch(c){
			case 'a':
				printf("-s --port:    server port\n");
				printf("-p --myport:  MySQL port\n");
				printf("-u --myuser:  MySQL user\n");
				printf("-w --mypwd:   MySQL password\n");
				printf("-h --myhost:  MySQL host\n");
				printf("-d --mydb:    MySQL database\n");
				printf("-t --time:    probe time\n");
				printf("-v --version: dispay version\n");
				printf("-a --help:    display this help\n");				
				exit(EXIT_SUCCESS);
				break;
			case 's':
				sockPort = atoi(optarg);
				break;
			case 'p':
				myPort = atoi(optarg);
				break;
			case 'u':
				myUser = optarg;
				break;
			case 'w':
				myPwd = optarg;
				break;
			case 'h':
				myHost = optarg;
				break;
			case 'd':
				myDB = optarg;
				break;
			case 't':
				timer = atof(optarg);
				break;
			case 'v':
				printf("*** otfdd version %s ***\n", OTFDD_VERSION);
				exit(EXIT_SUCCESS);
				break;
			case '?':
				break;
			default:
				abort();
		}
	}
}

//parse config file
void readConfFile(void){
	char buff[255], keyStr[20], valStr[200];
    int c, i, j;
	FILE *confFile;
	
	confFile = fopen(CONF_FILE, "r");
	
	if(!confFile){
		writeLog("EE:	open conf file failed");
		exit(EXIT_FAILURE);
	}

	while((c=fgetc(confFile)) != EOF){
		i = 0;
		j = 0;
		
		memset(keyStr, '\0', sizeof(keyStr));
		memset(valStr, '\0', sizeof(valStr));
		memset(buff, '\0', sizeof(buff));
		
		ungetc(c, confFile);
		fgets(buff, 80, confFile);
		
		//cherche '\n'
		char *p = strchr(buff, '\n'); 
		//si present
		if(p != NULL)
			*p = '\0' ; //bye
		
		while(i <= strlen(keyStr)){
			if(buff[i] != '=' && buff[i] != ' ')					
				keyStr[i] = buff[i];		
				
			i++;
		}
		
		while(i <= strlen(buff) && buff[i] != '\0'){
			if(buff[i] != '=' && buff[i] != ' '){
				valStr[j] = buff[i];
				j++;
			}
			
			i++;
		}
	
		if(strcmp(keyStr, "PORT") == 0 && sockPort == 0)
			sockPort = atoi(valStr);
		else if(strcmp(keyStr, "MYPORT") == 0 && myPort == 0)
			myPort = atoi(valStr);
		else if(strcmp(keyStr, "MYUSER") == 0 && myUser == NULL){
			myUser = malloc(strlen(valStr) + 1);
			strcpy(myUser, valStr);
		}
		else if(strcmp(keyStr, "MYPWD") == 0 && myPwd == NULL){
			myPwd = malloc(strlen(valStr) + 1);
			strcpy(myPwd, valStr);
		}
		else if(strcmp(keyStr, "MYHOST") == 0 && myHost == NULL){
			myHost = malloc(strlen(valStr) + 1);
			strcpy(myHost, valStr);
		}
		else if(strcmp(keyStr, "MYDB") == 0 && myDB == NULL){
			myDB = malloc(strlen(valStr) + 1);
			strcpy(myDB, valStr);
		}
		else if(strcmp(keyStr, "TIME") == 0)
			timer = atof(valStr);
	}
}
